﻿using CgdataBase;
using FilterCopyer.ViewModels;
using System;
using System.Diagnostics;
using System.Windows;

namespace FilterCopyer.Views
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel(this);
            WindowController.EscToExit(this);
        }

        private async void CheckAppVersion()
        {
            try
            {
                using (var client = new System.Net.Http.HttpClient())
                {
                    var app = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                    var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
                    var url = $"http://db.cnsos.net/get_app_version.php?app={app}&version={version}";
                    var result = await client.GetStringAsync(url);
                    if (result != version)
                    {
                        if (MessageBox.Show($"{Properties.Resources.ApplicationName}\r\n新版本：{result}\r\n当前版本：{version}\r\n点击【确认】打开程序文件夹", "发现新版本") == MessageBoxResult.OK)
                        {
                            await Process.Start("explorer.exe", System.AppDomain.CurrentDomain.BaseDirectory);
                            Application.Current.Shutdown();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
        }
    }
}